var request = require("request");
var colors = require('colors');

var iSuccessCount = 0;
var iFailCount = 0;

var arequestTime = [];

function tryRequest(url, period, times, time) {
    var options = {
        url: url,
        method: "GET"
    };
    if (!time)
        time = 1;

    var now = new Date();
    arequestTime[time] = now.getTime();
    console.log(now.toLocaleString() + " " + now.getMilliseconds() + " request " + time);

    var req = request(options, function(error, response, body) {
        var now = new Date();
        console.log(now.toLocaleString() + " " + now.getMilliseconds() + " response " + time + " +" + (now.getTime() - arequestTime[time]) + "ms");
        
        if (!error)
        {
            iSuccessCount++;
            console.log("          success ".green + iSuccessCount.toString().green + ":        " + JSON.stringify(response.headers));
        }
        else
        {
            iFailCount++;
            console.log("          fail ".red + iFailCount.toString().red + ":       " + JSON.stringify(error));
        }
    });

    if (time < times)
    {
        setTimeout(function() {
            tryRequest(url, period, times, time + 1);
        }, period);
    }
}

tryRequest("http://static.lianwo168.com/cstatic/js/util.js?version=122", 1, 500);
